package com.qfhd.soft.cjzcore.Core;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.TextUtils;

import com.qfhd.soft.cjzcore.UI.Core.CJPackage;

import java.util.List;

public class CJReceiver extends BroadcastReceiver {

    private static CJReceiver receiver = null;

    public static void register(Context context){
        try {
            if (receiver == null) {
                receiver = new CJReceiver();
                IntentFilter filter = new IntentFilter();
                // 安装
                filter.addAction("android.intent.action.PACKAGE_ADDED");
                filter.addDataScheme("package");// 这个加上才能接收到广播

//                filter.addAction(CJConstants.myBroast);

//                IntentFilter filter_t = new IntentFilter();
//                IntentFilter filter_user = new IntentFilter();
//                filter_t.addAction(ACTION_T);
//                filter_user.addAction(ACTION_USER);

                context.registerReceiver(receiver, filter);
//                context.registerReceiver(g_receiver, filter_t);
//                context.registerReceiver(g_receiver, filter_user);
            }
        } catch (Exception e) {
            // TODO: handle exception
            CJLogger.v("register error:" + e.toString());
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

//        if(intent.getAction().equals(CJConstants.myBroast)){
//            CJLogger.v("get loack Broadcast");
//            CJWatcher.getInstance().StartSevices();
//        }

        if (TextUtils.isEmpty(action))
            return;
        if (action.equalsIgnoreCase("android.intent.action.PACKAGE_ADDED")) {
            CJLogger.v("安装监听 intent.getDataString = " + intent.getDataString());
            String packageName = intent.getDataString().substring(8);
            CJLogger.v("PACKAGE_ADDED:" + packageName);

//            //有些广告源 无法检测下载安装（所以这里检测钻石奖励）
//            CJDiamond.getDiamondBtNet(CJConstants.ACTION_TYPE_DOWNLOAD);
//            CJDiamond.getDiamondBtNet(CJConstants.ACTION_TYPE_INSTALL);

            if(CJWatcher.getInstance().isHavePackage(packageName)){
                CJPackage.launchApk(context,packageName);
                CJWatcher.getInstance().setTime(packageName);
            }

            ActivityManager mActivityManager = (ActivityManager) context
                    .getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningAppProcessInfo> listOfProcesses = mActivityManager
                    .getRunningAppProcesses();

            //找到对应数据-珊瑚
//            CJKeyValue<CoinTask, StyleAdEntity> keyValue    = CJWatcher.getInstance().getValue(packageName);
//            if(keyValue == null) return;
//            CoinTask coinTask           = keyValue.getKey();
//            StyleAdEntity styleAdEntity = keyValue.getValue();
//            CJLogger.v("coinTask.task_type:"+coinTask.task_type);
//            if(coinTask.task_type == CJConstants.TMSDK_TYPE_DOWNLOAD){
//                CJTaskCenter.getInstance().updateStep(CJTaskCenter.onInstall,styleAdEntity,null);
//                CJWatcher.getInstance().removeWatch(packageName);
//                CJEvent.DownLoadDetail.getEventtBus().post(new CJEventObject(CJEventObjectType.OnInstall,null));
//            }
//            else if(coinTask.task_type == CJConstants.TMSDK_TYPE_VIDEO){
//                CJTaskCenter.getInstance().updateStep(CJTaskCenter.onInstall,styleAdEntity,null);
//                CJWatcher.getInstance().removeWatch(packageName);
//                CJEvent.VideoDetail.getEventtBus().post(new CJEventObject(CJEventObjectType.OnInstall,null));
//            }
//            else if(coinTask.task_type == CJConstants.TMSDK_TYPE_SIGN){
//                CJTaskCenter.getInstance().updateStep(CJTaskCenter.onInstall,styleAdEntity,null);
//                CJWatcher.getInstance().removeWatch(packageName);
//                CJEvent.Task.getEventtBus().post(new CJEventObject(CJEventObjectType.OnInstall,null));
//            }
        }
    }
}
